<template>
  <div class="article-comment">
    <!-- 全部评论 -->
    <van-sticky offset-top="11.73333vw">
      <div class="title van-hairline--bottom">
        <span>全部评论 ({{article.comm_count}})</span>
        <span>{{article.like_count}} 点赞</span>
      </div>
    </van-sticky>
    <!-- 评论列表 -->
    <div class="list">
      <van-list v-model="loading" :finished="finished" finished-text="没有评论了" @load="onLoad">
        <div class="item van-hairline--bottom" v-for="item in comments" :key="item.com_id">
          <van-image
            round
            width="10vw"
            height="10vw"
            :src="item.aut_photo"
          />
          <div class="info">
            <p>
              <span class="name">{{item.aut_name}}</span>
              <span class="zan">{{item.like_count}} <geek-icon name="like2" /></span>
            </p>
            <p class="cont">{{item.content}}</p>
            <p>
              <span class="reply" @click="clickReply(item)">{{item.reply_count}}回复 <i class="van-icon van-icon-arrow"></i></span>
              <span class="time">{{item.pubdate|relativeTime}}</span>
            </p>
          </div>
        </div>
      </van-list>
    </div>
    <!-- 底部工具 -->
    <div class="footer van-hairline--top">
      <div class="input" @click="showInput=true"><i class="van-icon van-icon-edit"></i></div>
      <div class="btn" @click="$emit('to-comment')"><geek-icon name="comment"></geek-icon><p>评论</p><i>{{article.comm_count}}</i></div>
      <div class="btn" @click="likeArticle()"><geek-icon :name="article.attitude===1?'like-sel':'like'"></geek-icon><p>点赞</p></div>
      <div class="btn" @click="collectArticle()"><geek-icon :name="article.is_collected?'collect-sel':'collect'"></geek-icon><p>收藏</p></div>
      <div class="btn"><geek-icon name="share"></geek-icon><p>分享</p></div>
    </div>
    <!-- 回复列表弹出框 -->
    <van-popup v-model="reply.open" position="right" @closed="currComment={}">
      <!-- 导航 -->
      <van-nav-bar left-arrow @click-left="reply.open=false" :title="`${currComment.reply_count}条回复`" />
      <!-- 列表 -->
      <div class="reply-wrapper list">
        <van-list :immediate-check="false" v-model="reply.loading" :finished="reply.finished" finished-text="没有回复了" @load="loadReply">
          <div class="item van-hairline--bottom" v-for="item in reply.list" :key="item.com_id">
            <van-image round width="10vw" height="10vw" :src="item.aut_photo"/>
            <div class="info">
              <p>
                <span class="name">{{item.aut_name}}</span>
                <span class="zan">{{item.like_count}} <geek-icon name="like2" /></span>
              </p>
              <p class="cont">{{item.content}}</p>
              <p><span class="time" style="margin-left:0">{{item.pubdate|relativeTime}}</span></p>
            </div>
          </div>
        </van-list>
      </div>
      <!-- 底部 -->
      <div class="footer van-hairline--top" style="position:static">
        <div class="input big" @click="showInput=true"><i class="van-icon van-icon-edit"></i></div>
        <div class="btn">
          <geek-icon name="collect"></geek-icon><p>收藏</p>
        </div>
        <div class="btn"><geek-icon name="share"></geek-icon><p>分享</p></div>
      </div>
    </van-popup>
    <!-- 写评论或者回复的弹出框 -->
    <van-popup v-model="showInput" position="bottom">
      <van-nav-bar
        left-arrow
        @click-left="showInput=false"
        :title="currComment.com_id?'回复评论':'评论文章'"
        right-text="发表"
        @click-right="submit()"
      />
      <!-- 输入框 rows输入几行 -->
      <van-field
        v-model="text"
        rows="3"
        type="textarea"
        maxlength="100"
        :placeholder="currComment.com_id?`@${currComment.aut_name}`:'请输入评论'"
        show-word-limit
      />
    </van-popup>
  </div>
</template>
<script>
import {
  getCommentsByArticle,
  likeArticle,
  collectArticle,
  getReplysByComment,
  commentOrReply
} from '@/api/article'
export default {
  name: 'ArticleComment',
  props: {
    article: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      loading: false,
      finished: false,
      // 偏移量
      offset: null,
      // 评论列表
      comments: [],
      // 回复相关数据
      currComment: {},
      reply: {
        open: false,
        loading: false,
        finished: false,
        offset: null,
        list: []
      },
      // 写评论或者回复需要的数据
      showInput: false,
      text: ''
    }
  },
  created () {
    // 显示加载效果
    this.loading = true
    // 触发加载
    this.onLoad()
  },
  methods: {
    // 提交评论，提交回复
    async submit () {
      if (!this.currComment.com_id) {
        // 提交评论
        // 1. 先校验下是否输入内容
        // 2. 调用API接口提交
        // 3. 失败：提示错误
        // 4. 成功：在评论列表顶部追加当前评论，评论数量累加1，清空输入的内容，关闭对话框，提示成功
        if (!this.text) return this.$toast('请输入评论内容')
        const [err, res] = await commentOrReply(this.article.art_id, this.text)
        if (err) return this.$toast.fail('评论失败')
        // res.data.data.new_obj 是后台返回的评论对象数据
        this.comments.unshift(res.data.data.new_obj)
        this.article.comm_count++
        this.text = ''
        this.showInput = false
        this.$toast.success('评论成功')
      } else {
        // 提交回复
        // 1. 先校验下是否输入内容
        // 2. 调用API接口提交
        // 3. 失败：提示错误
        // 4. 成功：在回复列表顶部追加当前回复，评论与回复数量累加1，清空输入的内容，关闭对话框，提示成功
        if (!this.text) return this.$toast('请输入回复内容')
        const [err, res] = await commentOrReply(this.currComment.com_id, this.text, this.article.art_id)
        if (err) return this.$toast.fail('回复失败')
        this.reply.list.unshift(res.data.data.new_obj)
        this.article.comm_count++
        this.currComment.reply_count++
        this.text = ''
        this.showInput = false
        this.$toast.success('回复成功')
      }
    },
    // 加载回复的函数
    async loadReply () {
      const [, res] = await getReplysByComment(this.currComment.com_id, this.reply.offset)
      if (res.data.data.end_id === res.data.data.last_id) {
        this.reply.finished = true
      } else {
        this.reply.offset = res.data.data.last_id
      }
      this.reply.list.push(...res.data.data.results)
      this.reply.loading = false
    },
    // 打开回复对话框
    clickReply (item) {
      // 记录当前评论数据对象
      this.currComment = item
      // 打开对话框
      this.reply.open = true
      // 自己触发加载
      this.reply.loading = true
      // 重置加载完毕
      this.reply.finished = false
      // 清空回复列表数据
      this.reply.list = []
      // 重置offset
      this.reply.offset = null
      // 加载数据
      this.loadReply()
    },
    // 收藏文章
    async collectArticle () {
      // 1. 确认收藏状态
      // 2. 发请求
      // 3. 失败错误提示
      // 4. 成功，更新状态，提示
      const newStatus = !this.article.is_collected
      const [err] = await collectArticle(this.article.art_id, newStatus)
      if (err) return this.$toast.fail('操作失败')
      this.article.is_collected = newStatus
      this.$toast.success('操作成功')
    },
    // 对文章点赞
    async likeArticle () {
      // 1. 确定你对文章做什么行为 attitude===1 已经点赞，行为：取消点赞
      // 2. 调用接口API函数
      // 3. 失败：提示
      // 4. 成功：更新当前点赞状态，提示
      if (this.article.attitude === 1) {
        // 取消点赞
        const [err] = await likeArticle(this.article.art_id, false)
        if (err) return this.$toast.fail('取消失败')
        this.article.attitude = -1
        this.article.like_count--
        this.$toast.success('取消成功')
      } else {
        // 点赞
        const [err] = await likeArticle(this.article.art_id, true)
        if (err) return this.$toast.fail('点赞失败')
        this.article.like_count++
        this.article.attitude = 1
        this.$toast.success('点赞成功')
      }
    },
    // 加载文章评论
    async onLoad () {
      // 1. 发请求，需要文章ID，和偏移量
      const [, res] = await getCommentsByArticle(this.article.art_id, this.offset)
      // 2. 需要判断是否加载完毕，end_id === last_id
      // 3. 加载完毕：finished true
      // 4. 没有加载完毕：需要设置偏移量为last_id  为下一次请求准备
      if (res.data.data.end_id === res.data.data.last_id) {
        this.finished = true
      } else {
        this.offset = res.data.data.last_id
      }
      // 5. 追加数据
      this.comments.push(...res.data.data.results)
      // 6. 加载中状态需要结束
      this.loading = false
    }
  }
}
</script>
<style scoped lang="less">
::v-deep .van-nav-bar__text {
  color: @geek-color;
}
::v-deep .van-field__control {
  background: @geek-gray-color;
  padding: 5px 10px;
  margin-bottom: 5px;
  border-radius: 4px;
}
.van-popup {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.reply-wrapper {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}
.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 50px;
  background: #fff;
  display: flex;
  width: 100%;
  align-items: center;
  .input {
    &.big {
      width: 260px;
    }
    margin-left: 10px;
    width: 200px;
    height: 34px;
    background: @geek-gray-color;
    border-radius: 17px;
    line-height: 36px;
    padding-left: 10px;
    .van-icon {
      color: #999;
    }
  }
  .btn {
    flex: 1;
    text-align: center;
    position: relative;
    p {
      margin: 0;
      font-size: 10px;
    }
    .geek-icon {
      font-size: 18px;
    }
    i {
      height: 16px;
      min-width: 16px;
      padding: 0 3px;
      background: @geek-color;
      color: #fff;
      font-size: 10px;
      position: absolute;
      right: 0;
      top: -4px;
      line-height: 16px;
      border-radius: 8px;
      font-style: normal;
    }
  }
}
.article-comment {
  .title {
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px;
    background: #fff;
    span {
      font-size: 16px;
      &:last-child {
        color: #ccc;
        font-size: 14px;
      }
    }
  }
  .list {
    padding: 0 16px;
    .item {
      display: flex;
      padding: 10px 0;
      .info {
        padding-left: 10px;
        flex: 1;
        p {
          margin: 0;
          .name {
            font-size: 16px;
          }
          .zan {
            font-size: 14px;
            float: right;
            color: #999;
            .geek-icon {
              font-size: 12px;
              position: relative;
              top: -1px;
            }
          }
          &.cont {
            font-size: 14px;
            color: #666;
            padding: 10px 0;
            word-break: break-all;
            padding-right: 40px;
          }
          .reply {
            min-width: 60px;
            height: 24px;
            text-align: center;
            line-height: 28px;
            font-size: 12px;
            background: @geek-gray-color;
            display: inline-block;
            border-radius: 14px;
            color: #666;
            .van-icon {
              position: relative;
              top: 1px;
            }
          }
          .time {
            font-size: 12px;
            color: #999;
            margin-left: 10px;
          }
        }
      }
    }
  }
}
</style>
